<template>
  <div style="padding: 15px;">
    <vxe-toolbar id="toolbarId" :setting="{storage: true}" export :refresh="{query: findList}">
      <template v-slot:buttons>
        <el-button @click="funcFilter">查询</el-button>
        <el-button @click="funcAdd">新增</el-button>
      </template>
    </vxe-toolbar>
    <vxe-table
      ref="xTable"
      border
      resizable
      stripe
      highlight-current-row
      highlight-hover-row
      row-id="id"
      height="700"
      :data="tableData"
      :loading="loading"
      remote-sort
      @sort-change="sortList"
    >
      <vxe-table-column>
        <vxe-table-column field="isDefault" title="是否默认卡" :formatter="formatterIsDefault" min-width="120" sortable />
        <template v-slot:header>
          <el-select v-model="listQuery.isDefault" placeholder="请选择" clearable>
            <el-option v-for="item in isDefaultOptions" :key="item.value" :label="item.label" :value="item.value" />
          </el-select>
        </template>
      </vxe-table-column>
      <vxe-table-column>
        <vxe-table-column field="userName" title="用户姓名" min-width="130" sortable />
        <template v-slot:header>
          <el-input v-model="listQuery.userName" placeholder="请输入" clearable />
        </template>
      </vxe-table-column>
      <vxe-table-column>
        <vxe-table-column field="userPhone" title="用户电话" min-width="130" sortable />
        <template v-slot:header>
          <el-input v-model="listQuery.userPhone" placeholder="请输入" clearable />
        </template>
      </vxe-table-column>
      <vxe-table-column>
        <vxe-table-column field="name" title="开户姓名" min-width="130" sortable />
        <template v-slot:header>
          <el-input v-model="listQuery.name" placeholder="请输入" clearable />
        </template>
      </vxe-table-column>
      <vxe-table-column>
        <vxe-table-column field="mobile" title="银行预留手机号" min-width="160" sortable />
        <template v-slot:header>
          <el-input v-model="listQuery.mobile" placeholder="请输入" clearable />
        </template>
      </vxe-table-column>
      <vxe-table-column>
        <vxe-table-column field="account" title="银行卡号" show-header-overflow show-overflow min-width="200" sortable />
        <template v-slot:header>
          <el-input v-model="listQuery.account" placeholder="请输入" clearable />
        </template>
      </vxe-table-column>
      <vxe-table-column>
        <vxe-table-column field="bankNo" title="开户银行" show-header-overflow show-overflow :formatter="formatterBank" min-width="250" sortable />
        <template v-slot:header>
          <el-input v-model="listQuery.bankNo" placeholder="请输入" clearable />
        </template>
      </vxe-table-column>
      <vxe-table-column>
        <vxe-table-column field="accountCity" title="开户城市" show-header-overflow show-overflow min-width="250" sortable />
        <template v-slot:header>
          <el-input v-model="listQuery.accountCity" placeholder="请输入" clearable />
        </template>
      </vxe-table-column>
      <vxe-table-column>
        <vxe-table-column field="branche" title="开户网点" show-header-overflow show-overflow min-width="200" sortable />
        <template v-slot:header>
          <el-input v-model="listQuery.branche" placeholder="请输入" clearable />
        </template>
      </vxe-table-column>
      <vxe-table-column>
        <vxe-table-column field="type" title="账户类型" show-header-overflow show-overflow :formatter="formatterType" min-width="130" sortable />
        <template v-slot:header>
          <el-select v-model="listQuery.type" placeholder="请选择" clearable>
            <el-option v-for="item in typeOptions" :key="item.value" :label="item.label" :value="item.value" />
          </el-select>
        </template>
      </vxe-table-column>
      <vxe-table-column>
        <vxe-table-column field="gmtCreated" title="创建时间" min-width="420" sortable :resizable="false" />
        <template v-slot:header>
          <el-date-picker
            v-model="gmtCreatedRange"
            type="daterange"
            value-format="yyyy-MM-dd"
            :picker-options="pickerOptions"
            style="width:100%"
            range-separator="至"

            start-placeholder="开始日期"
            end-placeholder="结束日期"
            @change="value => changeDate(value, 'gmtCreated')"
          />
        </template>
      </vxe-table-column>
      <vxe-table-column>
        <vxe-table-column field="gmtModified" title="修改时间" min-width="420" sortable :resizable="false" />
        <template v-slot:header>
          <el-date-picker
            v-model="gmtModifiedRange"
            type="daterange"
            value-format="yyyy-MM-dd"
            :picker-options="pickerOptions"
            style="width:100%"
            range-separator="至"

            start-placeholder="开始日期"
            end-placeholder="结束日期"
            @change="value => changeDate(value, 'gmtModified')"
          />
        </template>
      </vxe-table-column>
      <vxe-table-column title="操作" width="150" fixed="right">
        <template slot-scope="scope">
          <el-tooltip class="item" effect="dark" content="编辑" placement="top">
            <vxe-button type="text" @click="funcEdit(scope.row.code)">
              <i class="el-icon-edit" />
            </vxe-button>
          </el-tooltip>
          <el-tooltip class="item" effect="dark" content="删除" placement="top">
            <vxe-button type="text" @click="funcDelete(scope.row.code)">
              <i class="el-icon-delete" />
            </vxe-button>
          </el-tooltip>
        </template>
      </vxe-table-column>
    </vxe-table>
    <vxe-pager
      :loading="loading"
      :current-page.sync="listQuery.pageNum"
      :page-size.sync="listQuery.pageSize"
      :total="listQuery.total"
      :layouts="['PrevJump', 'PrevPage', 'Number', 'NextPage', 'NextJump', 'Sizes', 'FullJump', 'Total']"
      @page-change="handlePageChange"
    />

    <vxe-modal v-model="modelAddFlag" title="新增" width="800" height="780" resize>
      <compAdd ref="addRef" @fatherAddMethod="fatherAdd" @fatherCancelMethod="fatherCancel" />
    </vxe-modal>
    <vxe-modal v-model="modelEditFlag" title="编辑" width="800" height="780" resize>
      <compEdit ref="editRef" @fatherEditMethod="fatherEdit" @fatherCancelMethod="fatherCancel" />
    </vxe-modal>
  </div>
</template>
<script>
import Vue from 'vue'
import 'xe-utils'
import VXETable from 'vxe-table'
import 'vxe-table/lib/index.css'
import XEUtils from 'xe-utils'
import VXEUtils from 'vxe-utils'
Vue.use(VXETable, VXEUtils, XEUtils, { mounts: ['cookie'] })
import compAdd from './components/bankCardAdd.vue'
import compEdit from './components/bankCardEdit.vue'
import request from '@/utils/request'
import { getListDict } from '@/api/commonApi'
export default {
  name: 'BankCardList',
  components: {
    compAdd,
    compEdit
  },
  data() {
    return {
      pickerOptions: {
        shortcuts: [
          {
            text: '最近一周',
            onClick(picker) {
              const end = new Date()
              const start = new Date()
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 7)
              picker.$emit('pick', [start, end])
            }
          },
          {
            text: '最近一个月',
            onClick(picker) {
              const end = new Date()
              const start = new Date()
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 30)
              picker.$emit('pick', [start, end])
            }
          },
          {
            text: '最近三个月',
            onClick(picker) {
              const end = new Date()
              const start = new Date()
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 90)
              picker.$emit('pick', [start, end])
            }
          }
        ]
      },
      gmtCreatedRange: '',
      gmtModifiedRange: '',
      bankList: [],
      loading: false,
      tableData: [],
      modelAddFlag: false,
      modelEditFlag: false,
      listQuery: {
        pageNum: 1,
        pageSize: 10,
        sortField: '',
        sortType: '',
        total: 0
      },
      // 是否默认卡
      isDefaultOptions: [
        { label: '否', value: 0 },
        { label: '是', value: 1 }
      ],
      // 账户类型
      typeOptions: [
        { label: '个人账户', value: 1 },
        { label: '企业账户', value: 2 }
      ]
    }
  },
  created() {
    // 获取数据字典： 银行
    getListDict({ types: 'bank' }).then(response => {
      this.bankList = response.data.filter(item => item.type === 'bank')[0]['dictList']
      this.findList()
    })
  },
  methods: {
    findList() {
      this.loading = true
      return new Promise(resolve => {
        request({
          url: '/arpa-basic-api/bankCard',
          method: 'get',
          params: this.listQuery
        }).then(response => {
          this.tableData = response.data.records
          this.listQuery.total = response.data.total
          this.loading = false
          resolve();
        })
      })
    },
    // 时间
    changeDate(value, prefix) {
      if (value) {
        this.listQuery[`${prefix}Start`] = value[0] + " 00:00:00";
        this.listQuery[`${prefix}End`] = value[1] + " 23:59:59";
      } else {
        this.listQuery[`${prefix}Start`] = "";
        this.listQuery[`${prefix}End`] = "";
      }
    },
    // 查询
    funcFilter() {
      this.listQuery.pageNum = 1;
      this.findList();
    },
    // 排序
    sortList({ column, property, order }) {
      if (order) {
        this.listQuery.sortField = property
        this.listQuery.sortType = order
      } else {
        this.listQuery.sortField = ''
      }
      this.findList()
    },
    // 添加方法
    funcAdd() {
      this.$refs.addRef.getInfo()
      this.modelAddFlag = true
    },
    // 修改方法
    funcEdit(code) {
      this.$refs.editRef.getInfo(code)
      this.modelEditFlag = true
    },
    // 删除方法
    funcDelete(row) {
      this.$confirm('确认删除吗？').then(_ => {
        request({ url: `/arpa-basic-api/bankCard`, method: 'delete', params: { codes: row }}).then(response => {
          this.$message({ message: response.msg, type: 'success' })
          this.findList()
        })
      })
    },
    // 增加组件的父方法
    fatherAdd() {
      this.modelAddFlag = false
      this.findList()
    },
    // 编辑组件的父方法
    fatherEdit() {
      this.modelEditFlag = false
      this.findList()
    },
    // 翻译是否默认
    formatterIsDefault({ cellValue }) {
      const item = this.isDefaultOptions.find(
        item => item.value === cellValue
      )
      return item ? item.label : ''
    },
    // 翻译账户类型
    formatterType({ cellValue }) {
      const item = this.typeOptions.find(
        item => item.value === cellValue
      )
      return item ? item.label : ''
    },
    // 翻译开户银行
    formatterBank({ cellValue }) {
      const item = this.bankList.find(
        item => item.code === cellValue
      )
      return item ? item.name : ''
    },
    // 公共取消
    fatherCancel(modelFlag) {
      switch (modelFlag) {
        case 'add':
          this.modelAddFlag = false
          break
        case 'edit':
          this.modelEditFlag = false
          break
      }
    },
    // 分页handle
    handlePageChange({ currentPage, pageSize }) {
      this.listQuery.pageNum = currentPage
      this.listQuery.pageSize = pageSize
      this.findList()
    }
  }
}
</script>
